package com.bitcat.firstapp.demo.component

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bitcat.firstapp.R
import com.bitcat.firstapp.databinding.DialogCustomBinding
import com.bitcat.firstapp.utils.ToastUtil
import com.bitcat.firstapp.views.CustomDialog

class DialogDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_dialog_demo)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    fun showDialog1(view: View) {
        AlertDialog.Builder(this)
            .setTitle("温馨提示")
            .setMessage("您确定要删除吗？")
            .setPositiveButton("确定") { _, _ ->
                Toast.makeText(this, "确定点击了", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消") { _, _ ->
                Toast.makeText(this, "取消点击了", Toast.LENGTH_SHORT).show()
            }
            .show()
    }

    private var singleChoiceIndex = 0

    fun showDialog2(view: View) {
        val items = arrayOf("浅色主题", "深色主题", "系统默认")
        AlertDialog.Builder(this)
            .setTitle("请选择应用的主题样式")
            .setSingleChoiceItems(items, singleChoiceIndex) { dialog, which ->
                ToastUtil.showToast("您选择了${items[which]}")
                singleChoiceIndex = which
                dialog.dismiss()
            }
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }

            .show()
    }

    private var multiChoiceIndex = booleanArrayOf(false, false, false, false, false, false)

    fun showDialog3(view: View) {
        val items = arrayOf("阅读", "视频", "音乐", "游戏", "旅游","篮球")
        AlertDialog.Builder(this)
            .setTitle("请选择兴趣爱好")
            .setMultiChoiceItems(items,multiChoiceIndex) { dialog, which, isChecked ->
                //记录当前位置是否被选中
                multiChoiceIndex[which] = isChecked
            }
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
                val selectedItem = items.filterIndexed { index, s ->  multiChoiceIndex[index]}
                ToastUtil.showToast("您选择了${selectedItem.joinToString("-")}")
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }
            .show()
    }

    fun showDialog4(view: View) {
        val dialogBinding = DialogCustomBinding.inflate(layoutInflater)
        AlertDialog.Builder(this)
            .setTitle("用户信息")
            .setView(dialogBinding.root)
            .setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
                val username = dialogBinding.etUsername.text.toString()
                val phone = dialogBinding.etPhone.text.toString()
                val gender = when(dialogBinding.rgGender.checkedRadioButtonId){
                    R.id.rbMale -> "男"
                    R.id.rbFemale -> "女"
                    else -> "未知"
                }
                ToastUtil.showToast("用户名：$username 手机号：$phone 性别：$gender")
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }
            .show()
    }

    fun showDialog5(view: View) {
        val dialog = CustomDialog(this)
        dialog.setConfirmedListener {
            ToastUtil.showToast("您输入的内容是：$it")
        }
        dialog.show()

    }
}